<template>
    <div class="app-container">
        <el-row :gutter="20">
            <!--部门数据-->
            <el-col :span="4" :xs="24">

                <fileTrees @handleNodeClick="onTreeClick" :tableHeight="treeHeight" :isnoClick="isnoClick" :menuId="menuId">
                </fileTrees>
            </el-col>
            <!--离线接收数据-->
            <el-col :span="20" :xs="24" v-show="isShow">
<!--                <queryCriteriaList v-if="listFrom.length > 0" :showSearch="showSearch" :listFrom="listFrom"-->
<!--                                   @resetQuery="resetQuery" @handleQuery="handleQuery" ref="queryForm"></queryCriteriaList>-->


                <el-form ref="queryForm"  :inline="true" v-show="showSearch">
                    <el-form-item label="" v-for="(item, index) in listFrom" :key="index">
                        <span slot="label">{{ item.FIELD_CAPTION }} </span>
                        <input_item :item="item" :listFrom="listFrom" @onChangeTap="onChangeTap" :inputType="1" @handleQuery="handleQuery" :inputParams="queryInputParams" ></input_item>
                    </el-form-item>
                    <el-form-item  label="销毁状态">
                        <el-select v-model="destroyState">
                            <el-option :value="5" :label="'待销毁'" :selected="true">待销毁</el-option>
                            <el-option :value="8" :label="'已销毁'">已销毁</el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" icon="el-icon-search"  @click="handleQuery">搜索</el-button>
                        <el-button icon="el-icon-refresh"  @click="resetQuery">重置</el-button>
                    </el-form-item>
                </el-form>
                <el-row :gutter="10" class="mb8" ref="refbut" v-show="!((subStr == 'F' && categoryType == '3') || (subStr == 'F' && categoryType == '1'))">
                    <el-col :span="1.5">
                        <el-button type="primary" v-if="destroyState==5" plain icon="el-icon-takeaway-box"
                                   @click="onClickTap()">加入销毁申请单</el-button>
                        <el-button type="primary" v-if="destroyState==8" class="el-button el-button--danger el-button--medium is-plain" plain icon="el-icon-takeaway-box"
                                   @click="deleteDa()">删除</el-button>
                    </el-col>

                    <right-toolbar :showSearch.sync="showSearch" @queryTable="getlistPost"></right-toolbar>
                </el-row>
                <tableList v-if="tableHeight" :tableHeaderList="tableHeaderList" :isCZJD="false"
                           @handleSelectionChange="handleSelectionChange" :tableData="tableData" :loading="loading"
                           :tableHeight="tableHeight" @celldblclick="celldblclick" @sortChange="sortChange" ref="ref"></tableList>
                <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
                            :limit.sync="queryParams.pageSize" @pagination="getlistPost" />
            </el-col>

            <el-col :span="20" :xs="24" v-show="!isShow" class="empty">
                <el-empty :image-size="200"></el-empty>
            </el-col>

        </el-row>


        <!--start  组件详情 -->
        <jianDetailsOpen v-if="jianDetailsOpen" :open="jianDetailsOpen" :celldbData="celldbData"
                         @detailsHandleClose="detailsHandleClose">
        </jianDetailsOpen>
        <!--end 组件详情 -->
        <!-- start 组卷\项目详情 -->
        <juanDetailsOpen v-if="juanDetailsOpen" :open="juanDetailsOpen" :celldbData="celldbData"
                         @detailsHandleClose="detailsHandleClose"></juanDetailsOpen>
        <!-- end 组卷\项目详情 -->
    </div>
</template>
<script>
    import { getthbytreenodeHeaderList, getqueryfieldList } from "@/api/archive/fileCollection/offlinReception.js";
    import { getGroupTree } from "@/api/archive/archivesCategory.js"
    import { fpcz } from '@/api/archive/archivalAppraisal/destruction/destructionJD.js'
    import fileTrees from "@/components/fileTrees/index.vue"  //档案树
    import treeHeight from '@/mixins/treeHeight'//计算左侧树的高度
    import { getLastIndexOf } from "@/utils/index.js"
    import tableList from "@/components/tableList/index.vue"
    import jianDetailsOpen from '@/components/componentsJianDetails/index.vue' //件详情
    import juanDetailsOpen from '@/components/componentsJuanDetails/index.vue' //卷详情
    import queryCriteriaList from "@/components/queryCriteriaList/index.vue"  //查询列表
    import input_item from "@/components/input/index.vue"

    import { list, addDestruKu,deleteDa } from '@/api/archive/archivalAppraisal/destructionWarehouseManagement/destructionWarehouse.js' //只限销毁库用接口
    export default {
        mixins: [treeHeight], // 混入方法
        components: {
            fileTrees,
            tableList,
            jianDetailsOpen,
            juanDetailsOpen,
            queryCriteriaList,
            input_item
        },
        data() {
            return {
                loading: false,
                isnoClick: true, //防止树重复点击
                menuId: '',
                isShow: false, //是否显示右侧数据 isShow:true,  //判断  著录项目内件按钮是否显示
                // 显示搜索条件
                showSearch: true,
                queryParams:{},
                // 查询参数
                queryInputParams: {},
                total: 0,
                tableHeaderList: [], //table表头数据
                tableData: [],//table列表数据
                listFrom: [],//查詢列表字段
                jianDetailsOpen: false,//件详情弹窗
                juanDetailsOpen: false,//卷详情弹窗
                openInfo: { //件 卷  项目
                    opens: false,
                    categoryId: undefined,
                    menuId: undefined,
                    type: undefined
                },
                tableHeight: "",
                // 选中数组
                ids: [],
                // 非单个禁用
                single: true,
                // 非多个禁用
                multiple: true,
                subStr: '',
                categoryType: '',

                destroyState:5 //销毁状态
            }
        },
        created() {
            this.menuId = this.$route.meta.menuId
        },
        methods: {
           // 加入销毁申请单
            onClickTap() {
                if (!this.multiple) {
                    //this.$modal.confirm('是否确认加入销毁库？').then(() => {
                    let categoryType=this.categoryType
                    let categoryId=this.queryParams.categoryId
                    const params={}
                    let paramArr=[]
                    console.log(this.ids,typeof(this.ids) )
                    if(this.ids.indexOf(",")!=-1){
                        let daIdArray=this.ids.split(",");
                        daIdArray.forEach(function(item,index) {
                            let param={}
                            param.daId=item
                            param.daType=categoryType
                            param.categoryId=categoryId
                            paramArr.push(param)
                        })
                    }else{
                        let param={}
                        param.daId=this.ids
                        param.daType=this.categoryType
                        param.categoryId=this.queryParams.categoryId
                        paramArr.push(param)
                    }

                    params.addCarList=paramArr
                    const paramsJson=JSON.stringify(params)

                    console.log("paramsJson",paramsJson)
                    addDestruKu(paramsJson).then(res => {
                        if (res.code == "200") {
                            this.$modal.msgSuccess(res.msg);
                        }
                        this.getlistPost();
                    })

                    //}).catch(() => { });
                } else {
                    this.$message({ message: '请选择要添加的数据哦', type: 'warning' })
                }
            },
            deleteDa() {
                if (!this.multiple) {
                    //this.$modal.confirm('是否确认加入销毁库？').then(() => {
                    const params={}
                    params.ids=this.ids
                    params.categoryId=this.queryParams.categoryId
                    const paramsJson=JSON.stringify(params)
                    console.log("paramsJson",paramsJson)
                    deleteDa(paramsJson).then(res => {
                        if (res.code == "200") {
                            this.$modal.msgSuccess(res.msg);
                        }
                        this.getlistPost();
                    })
                    //}).catch(() => { });
                } else {
                    this.$message({ message: '请选择要删除的数据哦', type: 'warning' })
                }
            },
            onChangeTap(data) {
                var dataArr=""
                if(data.val!=undefined){
                    dataArr = data.queryParams;
                }else{
                    dataArr = data;
                }
                if (this.queryInputParams.length > 0) {
                    for (var i in this.queryInputParams) {
                        if (Object.keys(this.queryParams[i]).toString() == Object.keys(dataArr).toString()) {
                            this.queryInputParams.splice(i, 1)
                        }
                    }
                    this.queryInputParams = dataArr;
                } else {
                    this.queryInputParams = dataArr;
                }

            },
            handleQuery() {
                var searchcolumn = []
                for (var k in this.listFrom) {
                    for (var i in this.queryInputParams) {
                        if(this.queryInputParams[i] != null) {
                            if (this.listFrom[k].FIELD_NAME == i) {
                                if (this.listFrom[k].SHOWTYPE == 3&&this.listFrom[k].IS_QUERY==4) {
                                    searchcolumn.push({
                                        "queryType": this.listFrom[k].IS_QUERY,
                                        "showType": this.listFrom[k].SHOWTYPE,
                                        "column": this.listFrom[k].FIELD_NAME,
                                        "value": this.queryInputParams[i][0],
                                        "valueTwo": this.queryInputParams[i][1],
                                    })
                                } else if (this.listFrom[k].SHOWTYPE == 7 || this.listFrom[k].SHOWTYPE == 8) {
                                    searchcolumn.push({
                                        "queryType": this.listFrom[k].IS_QUERY,
                                        "showType": this.listFrom[k].SHOWTYPE,
                                        "column": this.listFrom[k].FIELD_NAME+"_ID",
                                        "value": this.queryInputParams[i+"_ID"],

                                    })
                                } else {
                                    searchcolumn.push({
                                        "queryType": this.listFrom[k].IS_QUERY,
                                        "showType": this.listFrom[k].SHOWTYPE,
                                        "column": this.listFrom[k].FIELD_NAME,
                                        "value": this.queryInputParams[i],

                                    })
                                }
                            }
                        }


                    }
                }





                this.queryParams['searchcolumn']=searchcolumn;
                this.queryParams['state']=this.destroyState
                console.log(this.queryParams,"3333333333333333333333")
                this.getlistPost();

            },







            //动态设置table的高度
            tableHeightData() {
                this.$nextTick(
                    () => {
                        let h = document.documentElement.clientHeight
                        //     console.log(this.$refs.queryForm.$el.offsetHeight, this.$refs.refbut.$el.offsetHeight, "sdklfjklliuhaiorng")
                        h = h - 200
                        if (this.$refs.queryForm) { // 页面上如果有面包屑区域徐减去它的高度
                            h = h - this.$refs.queryForm.$el.offsetHeight
                        }

                        if (this.$refs.refbut) { // 页面上如果有分页区域徐减去它的高度
                            h = h - this.$refs.refbut.$el.offsetHeight
                        }
                        // console.log(h,"sdjfdklsjfkl")
                        this.tableHeight = h > 310 ? h : 310 // 根据自己项目定高度

                        return h
                    }
                )

            },
            // 树节点单击事件
            onTreeClick(data) {
                var that = this;
                this.isnoClick = false;
                this.treeRowData = data;
                if (data.categoryId != null) {
                    this.isShow = true
                } else {
                    this.isShow = false
                }
                if (this.$refs.queryForm != undefined) {
                    this.$refs.queryForm.queryParams = {};
                }
                if (data.nodeType == null || data.nodeType == "t" || data.nodeType == "f") {
                    this.isnoClick = true;
                    return false;

                }
                this.loading = true;
                this.destroyState=5
                // this.queryParams = data;
                // console.log(data, "+++++++++++++++++")
                this.queryParams.state=this.destroyState
                this.queryParams.order = '';
                this.queryParams.pageNum = 1;
                this.queryParams.pageSize = 10;
                this.queryParams.categoryId = data.categoryId;
                this.queryParams.menuId = data.menuId;
                this.queryParams.propertyName = data.propertyName;
                this.queryParams.propertyValue = data.propertyValue;
                this.queryParams.subTableName = data.sub_table_name;
                if (this.queryParams.subTableName != undefined) {
                    this.subStr = getLastIndexOf(this.queryParams.subTableName);
                    let stb = this.queryParams.subTableName;
                    let stbArr = stb.split("_");
                    this.categoryType = stbArr[stbArr.length - 3];
                    that.$nextTick(res => {
                        this.getqueryfieldListPost();//点击树节点获取查询字段
                        this.getlistHeaderPost();/**点击树节点,获取table表头数据 */
                    })
                } else {
                    this.getGroupTreePost(data.categoryId);
                }




            },
            //档案门类树门类根据categoryID回显
            getGroupTreePost(ID) {
                getGroupTree(ID).then(response => {
                    console.log(response)
                    if (response.data.type == "0") { //件
                        this.queryParams.subTableName = response.data.fileTableName;
                    } else if (response.data.type == "1") { //卷
                        this.queryParams.subTableName = response.data.folderTableName;
                    } else if (response.data.type == "3") { //项目
                        this.queryParams.subTableName = response.data.folderTableName;
                    }
                    this.subStr = getLastIndexOf(this.queryParams.subTableName);
                    let stb = this.queryParams.subTableName;
                    let stbArr = stb.split("_");
                    this.categoryType = stbArr[stbArr.length - 3];
                    this.getqueryfieldListPost();//点击树节点获取查询字段
                    this.getlistHeaderPost();/**点击树节点,获取table表头数据 */
                });
            },



            /** 点击树节点获取查询字段 */
            getqueryfieldListPost() {
                this.queryParams.searchcolumn = undefined
                getqueryfieldList({
                    categoryId: this.queryParams.categoryId,
                    menuId: this.queryParams.menuId,
                    subTableName: this.queryParams.subTableName,
                }).then(response => {
                    this.listFrom = response.rows;
                });
            },

            /**点击树节点,获取table表头数据 */
            getlistHeaderPost() {
                getthbytreenodeHeaderList({
                        categoryId: this.queryParams.categoryId,
                        menuId: this.queryParams.menuId,
                        subTableName: this.queryParams.subTableName
                    }
                ).then(response => {
                    this.tableHeaderList = response.rows;
                    var fields = []
                    for (var i in response.rows) {
                        fields.push(response.rows[i].FIELD_NAME)
                    }
                    this.queryParams.fields = fields.toString()
                    this.getlistPost();
                });
            },
            /** 点击树节点,获取table数据*/
            getlistPost() {
                var that = this;
                this.loading = true;
                list(this.queryParams).then(response => {
                    this.tableData = response.rows;
                    this.total = response.total;
                    this.loading = false;
                    this.isnoClick = true;
                    this.tableHeightData();
                });
            },
            //是否选择数据
            handleSelectionChange(ids, single, multiple, selection) {
                this.ids = ids.toString();
                this.single = single;
                this.multiple = multiple;
                this.isValue = selection.every(item => item.CZTYPE === null);
            },

            //双击列表进入详情页面
            celldblclick(row) {
                var level = "";
                var label = "";
                console.log(this.queryParams.subTableName,"_____________")
                if (getLastIndexOf(this.queryParams.subTableName) == "F") {  //F 件
                    level = 0
                    label = row.TITLE;
                } else if (getLastIndexOf(this.queryParams.subTableName) == "V") { //卷
                    level = 1
                    label = row.TITLE;
                } else { //项目
                    level = 3
                    label = row.TITLE;
                }

                this.celldbData = {
                    id: row.ID,
                    categoryId: this.queryParams.categoryId,
                    menuId: this.queryParams.menuId,
                    sub_table_name: this.queryParams.subTableName,
                    label: label,
                    level: level,
                    ifSub_table_name: this.queryParams.subTableName,
                    isDel: "0",  //判断是回收站数据还是接收收据 isDel:1  回收站    isDel:0 //离线接收、档案接收、在线接收
                    tableHeaderList: this.tableHeaderList
                }
                this.$nextTick(() => {
                    if (this.subStr == "F") {  //F 进入件详情
                        this.jianDetailsOpen = true;
                    } else { //进入卷详情 和项目详情
                        this.juanDetailsOpen = true
                    }
                })

            },
            //关闭详情页面
            detailsHandleClose() {
                this.jianDetailsOpen = false;
                this.juanDetailsOpen = false;

            },

            //排序
            sortChange(val) {
                this.queryParams.order = val;
                this.getlistPost();
            },

            //点击搜索
            // handleQuery(searchcolumn) {
            //     var column = searchcolumn.filter(item => { return item.value !== '' && item.value !== null })
            //     this.queryParams.searchcolumn = column
            //     this.queryParams.order = undefined;
            //     this.getlistPost();
            //     this.$refs.ref.clearSort()
            // },
            //表单重置
            resetQuery() {

                this.queryParams.pageNum = 1
                this.queryParams.pageSize = 10;
                this.queryParams.searchcolumn = undefined
                this.queryParams.order = undefined;
                this.destroyState=5
                this.queryParams.state=5

                this.queryInputParams={}
                this.getlistPost();

            },

            //加入销毁申请单
            onClickTap(type) {
                if (!this.multiple) {
                    //this.$modal.confirm('是否确认加入销毁库？').then(() => {
                    let categoryType=this.categoryType
                    let categoryId=this.queryParams.categoryId
                    const params={}
                    let paramArr=[]
                    console.log(this.ids,typeof(this.ids) )
                    if(this.ids.indexOf(",")!=-1){
                        let daIdArray=this.ids.split(",");
                        daIdArray.forEach(function(item,index) {
                            let param={}
                            param.daId=item
                            param.daType=categoryType
                            param.categoryId=categoryId
                            paramArr.push(param)
                        })
                    }else{
                        let param={}
                        param.daId=this.ids
                        param.daType=this.categoryType
                        param.categoryId=this.queryParams.categoryId
                        paramArr.push(param)
                    }

                    params.addCarList=paramArr
                    const paramsJson=JSON.stringify(params)

                    console.log("paramsJson",paramsJson)
                    addDestruKu(paramsJson).then(res => {
                        if (res.code == "200") {
                            this.$modal.msgSuccess(res.msg);
                        }
                        this.getlistPost();
                    })

                    //}).catch(() => { });
                } else {
                    this.$message({ message: '请选择要添加的数据哦', type: 'warning' })
                }
            },



        }
    }
</script>
